package com.example.myapp;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapp.db.DBUtils;
import com.example.myapp.db.UserSQLiteOpenHelper;
import com.example.myapp.utils.Constants;
import com.example.myapp.utils.Tools;

public class LoginActivity  extends AppCompatActivity  implements View.OnClickListener {
    private ImageView login_back_img ;
    private  EditText login_username_et ;
    private EditText login_password_et ;
    private Button login_bt ;
    private TextView register_tv ;
    private TextView forget_password_tv ;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_layout);
          login_back_img = findViewById(R.id.login_back_img);
          login_username_et = findViewById(R.id.login_username_et);
          login_password_et = findViewById(R.id.login_password_et);
          login_bt = findViewById(R.id.login_bt);
          register_tv = findViewById(R.id.register_tv);
          forget_password_tv = findViewById(R.id.forget_password_tv);
         //控件监听
          login_back_img.setOnClickListener(this);
          login_bt.setOnClickListener(this);
          register_tv.setOnClickListener(this);
          forget_password_tv.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.login_back_img:
                //关闭
                finish();
                break;
            case  R.id.forget_password_tv:
                Toast.makeText(this,"还在开发中....",Toast.LENGTH_SHORT).show();
                break;
            case R.id.register_tv:
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.login_bt:
                userLogin();
                break;
        }

    }
   //用户登录
    private void userLogin() {
        //先校验用户和密码不能为空
        //获取用户名的内容
        String loginUserName = login_username_et.getText().toString().trim();
        //获取密码的内容
        String loginPassWord = login_password_et.getText().toString().trim();
        if (Tools.isEmpty(loginUserName)){
            Tools.toastTip(LoginActivity.this,"用户名不能为空！");
            return;
        }
        if (Tools.isEmpty(loginPassWord)){
            Tools.toastTip(LoginActivity.this,"密码不能为空！");
            return;
        }

        UserSQLiteOpenHelper helper= new UserSQLiteOpenHelper(LoginActivity.this, Constants.DATABASE_NAME);
        //只是查询用只读就够了
        SQLiteDatabase db = helper.getReadableDatabase();
        //校验登录用户名是否存在（是否已经注册），注册了用户名和密码是否匹配
        int userLoginStatus = DBUtils.userLoginStatus( db, loginUserName, loginPassWord);
        //不管成功都把数据库连接
        db.close();
        if (userLoginStatus==3){//用户密码登录成功跳到主页
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            //关闭登录界面
            finish();
        }else if (userLoginStatus==2){//用户输错密码，密码不匹配
            Tools.toastTip(LoginActivity.this,"密码输入错误，请重新输入！");
        }else { //用户名不存在，提示注册
            Tools.toastTip(LoginActivity.this,"用户名不存在，请先注册！");
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        String registerStatus = intent.getStringExtra("registerStatus");
        if ("Y".equals(registerStatus)){
            //如果是从注册成功跳转过来的，就把先把用户名和密码的输入框清空
            login_username_et.setText("");
            login_password_et.setText(""); ;
        }

    }
}
